package com.br.aiops.receiver.es.query;

import com.google.common.collect.Lists;
import lombok.Builder;
import lombok.Data;

import java.util.List;

@Data
@Builder
public class DSL {

    /**
     * 起止时间为内置变量
     */
    private static final String START_TIME_PLACEHOLDER = "\\$startTime";
    private static final String END_TIME_PLACEHOLDER = "\\$endTime";

    private String method;
    private String endpoint;
    private String body;

    /**
     * 逗号分隔
     */
    private String resultFields;

    public List<String> getResultFields() {
        if (resultFields.contains(",")) {
            return Lists.newArrayList(resultFields.split(","));
        }
        return Lists.newArrayList(resultFields);
    }
    /**
     * 替换时间变量
     * @param start
     * @param end
     * @return
     */
    public String getReplacedBody(long start, long end) {
        return body.replaceAll(START_TIME_PLACEHOLDER, String.valueOf(start)).replaceAll(END_TIME_PLACEHOLDER, String.valueOf(end));
    }
}
